package com.ebelter.bodyfatscale;

import android.os.Build;

import com.ebelter.bodyfatscale.common.Constants;
import com.ebelter.bodyfatscale.model.activitylifecycle.ActivityLifecycleCallbacksImp;
import com.ebelter.bodyfatscale.util.ToastUtil;
import com.ebelter.bodyfatscale.util.UserSpUtil;
import com.ebelter.btlibrary.BaseApplication;
import com.ebelter.btlibrary.util.EncryptUtil;
import com.ebelter.btlibrary.util.SpUtil;
import com.ebelter.btlibrary.util.ULog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.raizlabs.android.dbflow.config.FlowConfig;
import com.raizlabs.android.dbflow.config.FlowManager;
import com.tencent.bugly.crashreport.CrashReport;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.logging.Level;

/**
 * android应用基类，制定规则，配置app参数,
 * Created by pan on 2017/11/3.
 */

public class ApplicationHelper extends BaseApplication {

    public static ApplicationHelper mApplicationContecx;

    @Override
    public void onCreate() {
        super.onCreate();

        CrashReport.initCrashReport(getApplicationContext(), "10ba73e31d", true);

//        try {
//            String packgetName= getPackageName();
//            int minSdkVersion = this.getApplicationInfo().minSdkVersion;
//            int targetSdkVersion = this.getApplicationInfo().targetSdkVersion;
//            ULog.i("ApplicationHelper","---包名:"+packgetName+"---minSdkVersion="+minSdkVersion+"---targetSdkVersion="+targetSdkVersion);
//        }catch (Exception e){
//            e.printStackTrace();
//        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            String packgetName= getPackageName();
            int minSdkVersion = this.getApplicationInfo().minSdkVersion;
            int targetSdkVersion = this.getApplicationInfo().targetSdkVersion;
            ULog.i("ApplicationHelper","---包名:"+packgetName+"---minSdkVersion="+minSdkVersion+"---targetSdkVersion="+targetSdkVersion);
        }



        //设置异常捕获
      //  Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler());//捕获异常
        mApplicationContecx=this;
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacksImp());
        inits();
        SpUtil.writeLong(Constants.IUser.OPEN_APP_TIME,System.currentTimeMillis());//记录一下打开软件的时间



    }

    private void inits() {
        FlowManager.init(new FlowConfig.Builder(this).build());
        SpUtil.init(this);
        UserSpUtil.init(this);
        ToastUtil.init(this);
        EncryptUtil.init();
        initNet();
        closeAndroidPDialog();
    }

    private void closeAndroidPDialog(){
        try {
            Class aClass = Class.forName("android.content.pm.PackageParser$Package");
            Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class);
            declaredConstructor.setAccessible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Class cls = Class.forName("android.app.ActivityThread");
            Method declaredMethod = cls.getDeclaredMethod("currentActivityThread");
            declaredMethod.setAccessible(true);
            Object activityThread = declaredMethod.invoke(null);
            Field mHiddenApiWarningShown = cls.getDeclaredField("mHiddenApiWarningShown");
            mHiddenApiWarningShown.setAccessible(true);
            mHiddenApiWarningShown.setBoolean(activityThread, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //初始化网络
    private void initNet() {
        OkGo.init(this);
        OkGo.getInstance()
                // 打开该调试开关,打印级别INFO,并不是异常,是为了显眼,不需要就不要加入该行
                // 最后的true表示是否打印okgo的内部异常，一般打开方便调试错误
                .debug("OkGo", Level.INFO, true)
                //可以全局统一设置缓存模式,默认是不使用缓存,可以不传,具体其他模式看 github 介绍 https://github.com/jeasonlzy/
                .setCacheMode(CacheMode.NO_CACHE)
                .setConnectTimeout(6000)
                .setRetryCount(0);
             // .addCommonHeaders(getHttpCommonHeader()); //设置全局公共头
    }


//    public HttpHeaders getHttpCommonHeader() {
//
//        HttpHeaders headers = new HttpHeaders();
//        headers.put("Content-Type", "application/json");
//        return headers;
//    }
}